<!DOCTYPE html>
<html lang="zh-CN">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>字体预览</title>
    <style>
        body {
            font-family: Arial, sans-serif;
            margin: 0;
            padding: 20px;
            background-color: #f5f5f5;
        }
        .container {
            max-width: 800px;
            margin: 0 auto;
            background-color: white;
            padding: 30px;
            border-radius: 8px;
            box-shadow: 0 2px 10px rgba(0, 0, 0, 0.1);
        }
        h1 {
            color: #333;
            text-align: center;
            margin-bottom: 30px;
        }
        .controls {
            margin-bottom: 30px;
        }
        .form-group {
            margin-bottom: 20px;
        }
        label {
            display: block;
            margin-bottom: 8px;
            font-weight: bold;
            color: #555;
        }
        .font-select-container {
            display: flex;
            align-items: center;
            gap: 10px;
        }
        .font-select {
            padding: 10px;
            flex: 1;
            border: 1px solid #ddd;
            border-radius: 4px;
            font-size: 16px;
            background-color: white;
            cursor: pointer;
        }
        .font-nav-button {
            padding: 10px 15px;
            background-color: #2196F3;
            color: white;
            border: none;
            border-radius: 4px;
            cursor: pointer;
            font-size: 16px;
            transition: background-color 0.3s;
        }
        .font-nav-button:hover {
            background-color: #0b7dda;
        }
        .font-nav-button:disabled {
            background-color: #cccccc;
            cursor: not-allowed;
        }
        .custom-text-input {
            padding: 10px;
            width: 100%;
            border: 1px solid #ddd;
            border-radius: 4px;
            font-size: 16px;
            box-sizing: border-box;
        }
        .preview-container {
            margin-top: 30px;
            padding: 20px;
            border: 1px solid #ddd;
            border-radius: 8px;
            background-color: #f9f9f9;
        }
        .preview-title {
            font-size: 18px;
            font-weight: bold;
            margin-bottom: 15px;
            color: #333;
        }
        .preview-text {
            margin-top: 15px;
        }
        .preview-size-s {
            font-size: 18px;
            margin-bottom: 15px;
            padding: 10px;
            background-color: white;
            border-radius: 4px;
        }
        .preview-size-m {
            font-size: 24px;
            margin-bottom: 15px;
            padding: 10px;
            background-color: white;
            border-radius: 4px;
        }
        .preview-size-l {
            font-size: 36px;
            margin-bottom: 15px;
            padding: 10px;
            background-color: white;
            border-radius: 4px;
        }
        .no-fonts {
            text-align: center;
            color: #666;
            padding: 50px 0;
        }
        .font-file-name {
            font-family: monospace;
        }
        .selected-font-info {
            margin-top: 10px;
            font-size: 14px;
            color: #666;
            display: flex;
            align-items: center;
            gap: 10px;
        }
        .copy-button {
            padding: 4px 10px;
            background-color: #4CAF50;
            color: white;
            border: none;
            border-radius: 4px;
            cursor: pointer;
            font-size: 12px;
            transition: background-color 0.3s;
        }
        .copy-button:hover {
            background-color: #45a049;
        }
        .toast {
            position: fixed;
            top: 20px;
            right: 20px;
            background-color: #333;
            color: white;
            padding: 12px 20px;
            border-radius: 4px;
            opacity: 0;
            transition: opacity 0.3s;
            z-index: 1000;
            font-size: 14px;
        }
        .toast.show {
            opacity: 1;
        }
    </style>
    {% if fonts_data %}
    <style>
        {% for font in fonts_data %}
        @font-face {
            font-family: "{{ font.name|replace('"','&quot;') }}";
            src: url("/static/resources/{{ font.filename }}");
            font-display: swap;
        }
        {% endfor %}
    </style>
    {% endif %}
</head>
<body>
    <div class="container">
        <h1>字体预览工具</h1>
        
        {% if fonts_data %}
        <div class="controls">
            <div class="form-group">
                <label for="font-select">选择字体：</label>
                <div class="font-select-container">
                    <select id="font-select" class="font-select">
                    {% for font in fonts_data %}
                    <option value="{{ loop.index }}" data-font-name="{{ font.name }}">{{ font.filename }}</option>
                    {% endfor %}
                    </select>
                    <button id="prev-font" class="font-nav-button" onclick="navigateFont(-1)">上一个</button>
                    <button id="next-font" class="font-nav-button" onclick="navigateFont(1)">下一个</button>
                </div>
                <div id="selected-font-info" class="selected-font-info">
                    已选择：<span class="font-file-name">{{ fonts_data[0].name }}</span>
                    <button class="copy-button" onclick="copyFontName('{{ fonts_data[0].name }}')">复制</button>
                </div>
            </div>
            
            <div class="form-group">
                <label for="custom-text">自定义预览文本：</label>
                <input type="text" id="custom-text" class="custom-text-input" value="这是一段预览文本 ABCabc123">
            </div>
        </div>
        
        <div class="preview-container">
            <div class="preview-title">字体预览效果</div>
            <div class="preview-text">
                <div class="preview-size-s" id="preview-small">
                    小字体预览：这是一段预览文本 ABCabc123
                </div>
                <div class="preview-size-m" id="preview-medium">
                    中字体预览：这是一段预览文本 ABCabc123
                </div>
                <div class="preview-size-l" id="preview-large">
                    大字体预览：这是一段预览文本 ABCabc123
                </div>
            </div>
        </div>
        {% else %}
        <div class="no-fonts">
            <p>当前resources文件夹中没有找到字体文件。</p>
            <p>请将字体文件（.ttf, .otf, .woff, .woff2, .eot格式）放入 static/resources 文件夹中。</p>
        </div>
        {% endif %}
    </div>
    
    <script>
        // 条件执行 - 只有在有字体文件时才执行以下代码
        if (document.getElementById('font-select')) {
            // 自定义文本输入事件
            document.getElementById('custom-text').addEventListener('input', function() {
                const customText = this.value || '这是一段预览文本 ABCabc123';
                document.getElementById('preview-small').textContent = '小字体预览：' + customText;
                document.getElementById('preview-medium').textContent = '中字体预览：' + customText;
                document.getElementById('preview-large').textContent = '大字体预览：' + customText;
            });
            
            // 字体选择事件
                document.getElementById('font-select').addEventListener('change', function() {
                const fontName = this.options[this.selectedIndex].getAttribute('data-font-name');
                
                // 更新预览文本的字体
                const previewElements = [
                    document.getElementById('preview-small'),
                    document.getElementById('preview-medium'),
                    document.getElementById('preview-large')
                ];
                
                previewElements.forEach(el => {
                    // 直接使用字体名称作为font-family
                    el.style.fontFamily = fontName + ', sans-serif';
                });
                
                // 更新选中字体信息，包含复制按钮
                document.getElementById('selected-font-info').innerHTML = 
                    '已选择：<span class="font-file-name">' + fontName + '</span>' +
                    '<button class="copy-button" onclick="copyFontName(\'' + fontName + '\')">复制</button>';
            });
            
            // 初始化显示第一个字体
                window.onload = function() {
                    const previewElements = [
                        document.getElementById('preview-small'),
                        document.getElementById('preview-medium'),
                        document.getElementById('preview-large')
                    ];
                    
                    // 获取第一个字体的名称
                    const firstFontName = document.getElementById('font-select').options[0].getAttribute('data-font-name');
                    
                    previewElements.forEach(el => {
                        el.style.fontFamily = firstFontName + ', sans-serif';
                    });
                };
        }
        
        // 无论是否有字体文件，都为自定义文本输入框添加事件监听器
        const customTextInput = document.getElementById('custom-text');
        if (customTextInput) {
            customTextInput.addEventListener('input', function() {
                const customText = this.value || '这是一段预览文本 ABCabc123';
                const previewSmall = document.getElementById('preview-small');
                const previewMedium = document.getElementById('preview-medium');
                const previewLarge = document.getElementById('preview-large');
                
                if (previewSmall) previewSmall.textContent = '小字体预览：' + customText;
                if (previewMedium) previewMedium.textContent = '中字体预览：' + customText;
                if (previewLarge) previewLarge.textContent = '大字体预览：' + customText;
            });
        }
        // 字体导航函数
        function navigateFont(direction) {
            const fontSelect = document.getElementById('font-select');
            if (!fontSelect) return;
            
            const currentIndex = parseInt(fontSelect.value);
            const totalFonts = fontSelect.options.length;
            
            let newIndex;
            if (direction === -1) {  // 上一个
                newIndex = currentIndex > 1 ? currentIndex - 1 : totalFonts;
            } else {  // 下一个
                newIndex = currentIndex < totalFonts ? currentIndex + 1 : 1;
            }
            
            // 设置新的选中项
            fontSelect.value = newIndex;
            
            // 手动触发change事件，更新预览和字体信息
            const changeEvent = new Event('change');
            fontSelect.dispatchEvent(changeEvent);
        }
    
        // 复制字体名称函数
        function copyFontName(fontName) {
            try {
                // 直接复制传入的字体名称
                navigator.clipboard.writeText(fontName).then(() => {
                    // 显示toast提示
                    const toast = document.getElementById('toast');
                    toast.textContent = '已复制：' + fontName;
                    toast.classList.add('show');
                    
                    // 3秒后隐藏toast
                    setTimeout(() => {
                        toast.classList.remove('show');
                    }, 3000);
                }).catch(err => {
                    console.error('Clipboard API 复制失败:', err);
                    // 降级方案：使用传统的复制方法
                    fallbackCopyTextToClipboard(fontName);
                });
            } catch (error) {
                console.error('复制函数执行错误:', error);
                fallbackCopyTextToClipboard(fontName);
            }
        }
        
        // 降级复制方案，当Clipboard API不支持时使用
        function fallbackCopyTextToClipboard(text) {
            try {
                const textArea = document.createElement('textarea');
                textArea.value = text;
                textArea.style.position = 'fixed';
                textArea.style.left = '-999999px';
                textArea.style.top = '-999999px';
                document.body.appendChild(textArea);
                textArea.focus();
                textArea.select();
                
                const success = document.execCommand('copy');
                if (success) {
                    const toast = document.getElementById('toast');
                    toast.textContent = '已复制：' + text;
                    toast.classList.add('show');
                    setTimeout(() => {
                        toast.classList.remove('show');
                    }, 3000);
                } else {
                    console.error('降级复制命令执行失败');
                }
            } catch (err) {
                console.error('降级复制也失败:', err);
            } finally {
                // 确保无论成功失败都移除textarea
                const textArea = document.querySelector('textarea[style*="-999999px"]');
                if (textArea && document.body.contains(textArea)) {
                    document.body.removeChild(textArea);
                }
            }
        }
    </script>
    
    <!-- Toast提示元素 -->
    <div id="toast" class="toast"></div>
</body>
</html>